package view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.util.LinkedList;
import java.util.List;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
import qw.easyFrame.throwables.DefinedRuntimeException;

/**
 * 包装JTextPane.
 * @author Rex
 *
 */
public class EasyTextPane extends JTextPane {
	private static final long serialVersionUID = 20240922L;

	private StyledDocument doc;
	
	private List<String> texts;

	public EasyTextPane(Font font) {
		super();
		this.setEditable(false);
		this.setFont(font);
		this.doc = this.getStyledDocument();
		this.texts = new LinkedList<>();
	}

	/**
	 * @param text      文本内容
	 * @param color     文本颜色
	 */
	private void append(String text, Color color) {
		SimpleAttributeSet set = new SimpleAttributeSet();
		StyleConstants.setForeground(set, color);// 设置文本颜色
		StyleConstants.setAlignment(set, StyleConstants.ALIGN_LEFT);// 设置文本对齐方式
		
		synchronized (this) {
			int length = this.getText().length();
			int docLen = doc.getLength();
			doc.setParagraphAttributes(length, docLen - length, set, false);
			try {
				text = text.concat("\r\n");
				doc.insertString(docLen, text, set);// 插入文本
				texts.add(text);
			} catch (BadLocationException e) {
				throw new DefinedRuntimeException(false, "插入文本失败");
			}
		}
	}
	
	/**
	 * @param text 文本内容
	 */
	public void append(String text) {
		append(text, Color.BLACK);
	}
	
	/**
	 * @param text 文本内容
	 */
	public void appendErrMsg(String errMsg) {
		append(errMsg, Color.RED);
	}
	
	/**
	 * 清空文本.
	 */
	public synchronized void clear() {
		try {
			doc.remove(0, doc.getLength());
			texts.clear();
		} catch (BadLocationException e) {
			throw new DefinedRuntimeException(false, "清空文本失败");
		}
	}
	
	@Override
	public boolean getScrollableTracksViewportWidth() {
		// 当viewPort的宽度大于EditorPane的宽度时，才设置跟随viewPort的宽度
		return getUI().getPreferredSize(this).width <= getParent().getSize().width;
	}

	@Override
	public Dimension getPreferredSize() {
		// 禁止特性：当viewPort的宽度小于EditorPane的最小宽度时，getPreferredSize返回的是minWidth
		return getUI().getPreferredSize(this);
	}
	
	/**
	 * 绑定默认右键菜单.
	 * @param font
	 */
	public void defaultRightClickMenu(Font font) {
		RightClickMenu menu = new RightClickMenu(this, font);
		menu.addItem("清空", e -> clear());
		// 系统剪贴板
		Clipboard board = Toolkit.getDefaultToolkit().getSystemClipboard();
		menu.addItem("复制全部", e -> board.setContents(new StringSelection(toString()), null));
		addMouseListener(menu);
	}
	
	/**
	 * 返回文本域内的所有文字.
	 */
	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();

		synchronized (this) {
			final List<String> list = this.texts;
			for (String str : list)
				builder.append(str);
		}
		return builder.toString();
	}

}